﻿namespace SDFL.Helper
{
    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Web.Script.Serialization;
    using System.Xml;
    using System.Xml.Serialization;

    public class SerializationHelper
    {
        public static object BinaryDeserialize(Stream stream)
        {
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                return formatter.Deserialize(stream);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static object BinaryDeserialize(byte[] bytes)
        {
            try
            {
                MemoryStream serializationStream = new MemoryStream(bytes);
                object obj2 = new BinaryFormatter().Deserialize(serializationStream);
                serializationStream.Close();
                return obj2;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static object BinaryDeserialize(string path)
        {
            try
            {
                if (!File.Exists(path))
                {
                    return null;
                }
                StreamReader reader = new StreamReader(path);
                object obj2 = new BinaryFormatter().Deserialize(reader.BaseStream);
                reader.Close();
                return obj2;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static byte[] BinarySerialize(object obj)
        {
            try
            {
                MemoryStream serializationStream = new MemoryStream();
                new BinaryFormatter().Serialize(serializationStream, obj);
                byte[] buffer = serializationStream.ToArray();
                serializationStream.Close();
                return buffer;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool BinarySerialize(Stream stream, object obj)
        {
            try
            {
                new BinaryFormatter().Serialize(stream, obj);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool BinarySerialize(string path, object obj)
        {
            try
            {
                StreamWriter writer = new StreamWriter(path);
                new BinaryFormatter().Serialize(writer.BaseStream, obj);
                writer.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static T JsonDeserialize<T>(string json)
        {
            return JsonDeserialize<T>(json, 100);
        }

        public static T JsonDeserialize<T>(string json, int recursionLimit)
        {
            return new JavaScriptSerializer { RecursionLimit = recursionLimit }.Deserialize<T>(json);
        }

        public static string JsonSerialize(object obj)
        {
            return JsonSerialize(obj, 100);
        }

        public static string JsonSerialize(object obj, int recursionLimit)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            return new JavaScriptSerializer { RecursionLimit = recursionLimit }.Serialize(obj);
        }

        public static string ToJson(object obj)
        {
            return ToJson(obj, 100);
        }

        public static string ToJson(object obj, int recursionLimit)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            return new JavaScriptSerializer { RecursionLimit = recursionLimit }.Serialize(obj);
        }

        public static T XmlDeserialize<T>(byte[] bytes)
        {
            T local;
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            MemoryStream input = new MemoryStream(bytes);
            XmlReader xmlReader = XmlReader.Create(input);
            try
            {
                if (!serializer.CanDeserialize(xmlReader))
                {
                    return default(T);
                }
                local = (T)serializer.Deserialize(xmlReader);
            }
            catch (Exception)
            {
                local = default(T);
            }
            finally
            {
                if (xmlReader.ReadState != ReadState.Closed)
                {
                    xmlReader.Close();
                }
            }
            return local;
        }

        public static T XmlDeserialize<T>(string xmlPath)
        {
            T local;
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            XmlTextReader xmlReader = new XmlTextReader(xmlPath);
            if (!File.Exists(xmlPath))
            {
                return default(T);
            }
            try
            {
                if (!serializer.CanDeserialize(xmlReader))
                {
                    return default(T);
                }
                local = (T)serializer.Deserialize(xmlReader);
            }
            catch (Exception)
            {
                local = default(T);
            }
            finally
            {
                if (xmlReader.ReadState != ReadState.Closed)
                {
                    xmlReader.Close();
                }
            }
            return local;
        }

        public static object XmlDeserialize(string xmlPath, Type type)
        {
            object obj2;
            XmlSerializer serializer = new XmlSerializer(type);
            XmlTextReader xmlReader = new XmlTextReader(xmlPath);
            if (!File.Exists(xmlPath))
            {
                return null;
            }
            try
            {
                if (!serializer.CanDeserialize(xmlReader))
                {
                    return null;
                }
                obj2 = serializer.Deserialize(xmlReader);
            }
            catch (Exception)
            {
                obj2 = null;
            }
            finally
            {
                if (xmlReader.ReadState != ReadState.Closed)
                {
                    xmlReader.Close();
                }
            }
            return obj2;
        }

        public static object XmlDeserialize(byte[] bytes, Type type)
        {
            XmlSerializer serializer = new XmlSerializer(type);
            MemoryStream input = new MemoryStream(bytes);
            if (bytes.Length == 0)
            {
                return null;
            }
            try
            {
                XmlReader xmlReader = XmlReader.Create(input);
                if (!serializer.CanDeserialize(xmlReader))
                {
                    return null;
                }
                return serializer.Deserialize(xmlReader);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static byte[] XmlSerialize(object obj)
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            try
            {
                serializer.Serialize((Stream)stream, obj);
            }
            catch (Exception)
            {
            }
            return stream.ToArray();
        }

        public static bool XmlSerialize(string xmlPath, object obj)
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            XmlTextWriter writer = new XmlTextWriter(xmlPath, Encoding.UTF8)
            {
                Formatting = Formatting.Indented
            };
            try
            {
                serializer.Serialize((XmlWriter)writer, obj);
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (writer.WriteState != WriteState.Closed)
                {
                    writer.Close();
                }
            }
            return true;
        }

        public static string XmlSerializeToString(object obj)
        {
            string str = string.Empty;
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            try
            {
                serializer.Serialize((Stream)stream, obj);
                str = Encoding.UTF8.GetString(stream.ToArray());
            }
            catch (Exception)
            {
            }
            return str;
        }
    }
}
